// pages/statistics/index.js
var app = getApp();
var lineChart, pieChart;
var wxCharts =  require('../../util/wxcharts-min.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info: {},
    type: "date",//date,month
    showhighchartstype: 'line',
    date: '',
    dateForShow:'',
    data: {},
    maxDate: '',
    pie_chart: {},
    line_chart: {},
    statisticsBasicInfo: {},
    shop_id: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.maxDate = "2019-01," + this.GetDateStr(0);
    this.getData();
    this.setData({ date: this.GetDateStr(0) });
    this.dateForShowF();
  },
  showCondtion: function (e) {
    var type = e.currentTarget.dataset.type;
     this.setData({
       type: type
     });
    this.getData();
    this.dateForShowF();
  },
  bindDateChange:function(e){
    var date = e.detail.value;
    this.setData({ date: date});
    this.getData();
    this.dateForShowF();
    // console.log(e);
  },
  dateForShowF:function(){
    var date = this.data.date;
    var dateArr = date.split("-");
    var showDate="";
    
    if (this.data.type =="month"){
      showDate = dateArr[0] + "-" + dateArr[1]
    }else{
      showDate = date;
    }
    this.setData({ dateForShow: showDate});
  },
  GetDateStr: function (AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;//获取当前月份的日期
    var d = dd.getDate();
    return y + "-" + m + "-" + d;
  },
  getData: function () {
    var that = this;
    app.api("apiv2/service_provider_order/statisticsBasicInfo", { date: this.data.date, type: this.data.type }, function (res) {
      var data = res.data;
      that.setData({ statisticsBasicInfo:data});
       
    })
    //  x统计数据
    var url = "apiv2/service_provider_order/statisticsByDay";
    if (this.data.type == "month") {
      url = "apiv2/service_provider_order/statisticsByMonth";
    }

    app.api(url, { date: this.data.date }, function (res) {
      var data = res.data;
      that.setData({
         data:data,       
      });
      that.dateForShowF();
      that.changeToHighcharts();

    })
  },
  changeToHighcharts: function () {
    var HighchartsData = this.data.data;
    // console.log(HighchartsData);
    // return ;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth-20;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }
    //   /************折线形图*************/
    var categories = [];
    var data = [];
    var i;
    for ( i in HighchartsData.bar_chart) {
      if (HighchartsData.bar_chart[i].title){
        categories.push(HighchartsData.bar_chart[i].title);
        data.push(HighchartsData.bar_chart[i].order_amount);
      }
  
    }
    let line = {
      canvasId: 'highcharts-line', // canvas-id
        type: 'line', // 图表类型，可选值为pie, line, column, area, ring
        categories: categories,
        series: [{ // 数据列表
          name: '时间',
          data: data
        }],
      xAxis: {
        disableGrid: true,
        min: 0
      },
      animation: true,
      yAxis: {
        title: '成交金额 (元)',
        min: 0
      },
        width: windowWidth,
        height: 300,
        dataLabel: false, // 是否在图表中显示数据内容值
      }
    this.data.line_options = line;
    //   /************饼形图*************/
  //   var series = [];
  //   for (i in HighchartsData.pie_chart) {
 
  //       series.push({
  //         name: HighchartsData.pie_chart[i].title,
  //         data: HighchartsData.pie_chart[i].order_amount
  //       });
  //   }
  //   var pie = {
  //     canvasId: 'highcharts-pie',
  //       type: 'pie',
  //       series: series,
  //      width: windowWidth,
  //      height: 300,
  //       dataLabel: true,
  //  }
  //   this.data.pie_options = pie;
    this.showhighcharts();
  },
  touchHandler: function (e) {
    var tmplineChart;
    if (e.target.dataset.name == "pie") {
      tmplineChart = pieChart;
    } else {
      tmplineChart = lineChart;
    }
    tmplineChart.scrollStart(e);
  },
  moveHandler: function (e) {
    var tmplineChart;
    if (e.target.dataset.name == "pie") {
      tmplineChart = pieChart;
    } else {
      tmplineChart = lineChart;
    }
    tmplineChart.scroll(e);
  },
  touchEndHandler: function (e) {
    var tmplineChart;
    if (e.target.dataset.name == "pie"){
       tmplineChart = pieChart;
    }else{
      tmplineChart = lineChart;
    }
    console.log(tmplineChart);
    tmplineChart.scrollEnd(e);
    tmplineChart.showToolTip(e, {
      format: function (item, category) {
        console.log(item);
        return category + '成交金额 ：' + item.data+"元";
      }
    });
  },

  showhighcharts: function (e) {
    var type = e ? e.currentTarget.dataset.type : '';
    if (type) {
      
      this.setData({ showhighchartstype: type});
    }
    if (this.data.showhighchartstype == "line") {
      console.log("line_options", this.data.line_options);
      lineChart =  new wxCharts(this.data.line_options);
    } else {
      console.log("pie_options", this.data.pie_options);
      // return;
      pieChart = new wxCharts(this.data.pie_options);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})